/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.TableModel.TechnicienFormControleur;
import Controleur.TypeFormulaire;
import static Controleur.TypeFormulaire.AFFICHAGE;
import static Controleur.TypeFormulaire.CREATION;
import static Controleur.TypeFormulaire.EDITION;
import Controleur.TypeIncFormControleur;
import Modele.Technicien;
import Modele.TypeIncident;
import Utils.Constantes;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

/**
 *
 * @author Ben
 */
public class TechnicienForm extends javax.swing.JDialog {
    
     private TypeFormulaire mode;
    private ArrayList<JCheckBox> cb_outils;
    private TechnicienFormControleur controleur;
    private Technicien technicien;

    /**
     * Creates new form TechnicienForm
     */
    public TechnicienForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode) {
        super(parent, modal);
        initComponents();
        
        //initialisation du controleur
        controleur = new TechnicienFormControleur(this);
        
        this.mode = mode;
        cb_outils = new ArrayList<>();
        for (String s : Constantes.OUTILS) {
            JCheckBox cb = new JCheckBox(s);
            cb_outils.add(cb);
            panel_materiel.add(cb);
        }
        // on active les controles en fonction du mode
        appliquerMode();
    }
    
    /**
     * Creates new form TypeIncidentForm
     */
    public TechnicienForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode, Technicien technicien) {
        this(parent, modal, mode);
        this.technicien = technicien;
        if (mode != TypeFormulaire.CREATION) {
            tf_nom.setText(technicien.getMNom());
            tf_prenom.setText(technicien.getMPrenom());
            checkOutils(technicien.getMEquipement());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_titre = new javax.swing.JLabel();
        label_nom = new javax.swing.JLabel();
        label_prenom = new javax.swing.JLabel();
        label_equipement = new javax.swing.JLabel();
        tf_nom = new javax.swing.JTextField();
        tf_prenom = new javax.swing.JTextField();
        panel_boutons = new javax.swing.JPanel();
        btn_annuler = new javax.swing.JButton();
        btn_enregistrer = new javax.swing.JButton();
        btn_activ_modif = new javax.swing.JButton();
        btn_update = new javax.swing.JButton();
        panel_materiel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        label_titre.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        label_titre.setText("Technicien");

        label_nom.setLabelFor(tf_nom);
        label_nom.setText("Nom");

        label_prenom.setLabelFor(tf_prenom);
        label_prenom.setText("Prénom");

        label_equipement.setText("Equipement");

        tf_nom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_nomActionPerformed(evt);
            }
        });

        tf_prenom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_prenomActionPerformed(evt);
            }
        });

        btn_annuler.setText("Annuler");
        btn_annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_annulerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_annuler);

        btn_enregistrer.setText("Enregistrer");
        btn_enregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_enregistrerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_enregistrer);

        btn_activ_modif.setText("Modifier");
        btn_activ_modif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_activ_modifActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_activ_modif);

        btn_update.setText("Enregistrer");
        btn_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_updateActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_update);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_prenom, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_nom, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(95, 95, 95))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(panel_materiel, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panel_boutons, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label_nom)
                            .addComponent(label_prenom)
                            .addComponent(label_equipement))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(label_titre, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_titre, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_nom)
                    .addComponent(tf_nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_prenom)
                    .addComponent(tf_prenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(label_equipement)
                .addGap(11, 11, 11)
                .addComponent(panel_materiel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_boutons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_nomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_nomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_nomActionPerformed

    private void tf_prenomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_prenomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_prenomActionPerformed

    private void btn_annulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_annulerActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_annulerActionPerformed

    private void btn_enregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_enregistrerActionPerformed

        if(tf_nom.getText().equals("") ||
            tf_prenom.getText().equals("")){
            JOptionPane.showMessageDialog(this,
                "Champ manquant", "Erreur", JOptionPane.WARNING_MESSAGE);
        }else{
            btn_enregistrer.setEnabled(false);
            //création de la chaine d'outils
            StringBuilder builder = new StringBuilder();
            for(int i = 0 ; i < cb_outils.size() ; i++){
                if(cb_outils.get(i).isSelected()){
                    builder.append(cb_outils.get(i).getText());
                    if(i != cb_outils.size()-1){
                        builder.append(Constantes.SPLIT);
                    }
                }
            }
            technicien.setMNom(tf_nom.getText());
            technicien.setMPrenom(tf_prenom.getText());
            technicien.setMEquipement(builder.toString());
            controleur.enregistrer(technicien);
        }

    }//GEN-LAST:event_btn_enregistrerActionPerformed

    private void btn_activ_modifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_activ_modifActionPerformed
        mode = TypeFormulaire.EDITION;
        appliquerMode();
    }//GEN-LAST:event_btn_activ_modifActionPerformed

    private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_updateActionPerformed

        if(tf_nom.getText().equals("") ||
            tf_prenom.getText().equals("")){
            JOptionPane.showMessageDialog(this,
                "Champ manquant", "Erreur", JOptionPane.WARNING_MESSAGE);
        }else{
            btn_update.setEnabled(false);
            //création de la chaine d'outils
            StringBuilder builder = new StringBuilder();
            for(int i = 0 ; i < cb_outils.size() ; i++){
                if(cb_outils.get(i).isSelected()){
                    builder.append(cb_outils.get(i).getText());
                    if(i != cb_outils.size()-1){
                        builder.append(Constantes.SPLIT);
                    }
                }
            }
            technicien.setMNom(tf_nom.getText());
            technicien.setMPrenom(tf_prenom.getText());
            technicien.setMEquipement(builder.toString());
            controleur.modifier(technicien);
        }
    }//GEN-LAST:event_btn_updateActionPerformed

    
    private void appliquerMode() {
        switch (mode) {
            case AFFICHAGE:
                activerChamps(false);
                //activation des boutons
                btn_activ_modif.setVisible(true);
                btn_annuler.setVisible(false);
                btn_enregistrer.setVisible(false);
                btn_update.setVisible(false);
                break;
            case CREATION:
                activerChamps(true);
                //activation des boutons
                btn_activ_modif.setVisible(false);
                btn_annuler.setVisible(true);
                btn_enregistrer.setVisible(true);
                btn_update.setVisible(false);
                break;
            case EDITION:
                activerChamps(true);
                //activation des boutons
                btn_activ_modif.setVisible(false);
                btn_annuler.setVisible(true);
                btn_enregistrer.setVisible(false);
                btn_update.setVisible(true);
                break;
            default:
                break;
        }
    }

    private void activerChamps(boolean bool) {
        tf_nom.setEditable(bool);
        tf_prenom.setEditable(bool);
        for (JCheckBox cb : cb_outils) {
            cb.setEnabled(bool);
        }
    }

    private void checkOutils(String s) {
        String outils[] = s.split(Constantes.SPLIT);
        for (String outil : outils) {
            for (JCheckBox cb : cb_outils) {
                if (outil.equals(cb.getText())) {
                    cb.setSelected(true);
                }
            }
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TechnicienForm dialog = new TechnicienForm(new javax.swing.JFrame(), true, AFFICHAGE);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_activ_modif;
    private javax.swing.JButton btn_annuler;
    private javax.swing.JButton btn_enregistrer;
    private javax.swing.JButton btn_update;
    private javax.swing.JLabel label_equipement;
    private javax.swing.JLabel label_nom;
    private javax.swing.JLabel label_prenom;
    private javax.swing.JLabel label_titre;
    private javax.swing.JPanel panel_boutons;
    private javax.swing.JPanel panel_materiel;
    private javax.swing.JTextField tf_nom;
    private javax.swing.JTextField tf_prenom;
    // End of variables declaration//GEN-END:variables
}
